/**
 * This file is part of the eConference project and it is distributed under the 
 * terms of the MIT Open Source license.
 * 
 * The MIT License
 * Copyright (c) 2005 Collaborative Development Group - Dipartimento di Informatica, 
 *                    University of Bari, http://cdg.di.uniba.it
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software 
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to the following 
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package it.uniba.di.cdg.jabber.internal;

import it.uniba.di.cdg.jabber.JabberBackend;
import it.uniba.di.cdg.xcore.chat.IChatMessage;
import it.uniba.di.cdg.xcore.chat.IChatService.ChatContext;
import it.uniba.di.cdg.xcore.chat.IChatService.IChatListener;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jmock.Mock;
import org.jmock.cglib.MockObjectTestCase;

/**
 * jUnit test case for {@see it.uniba.di.cdg.jabber.internal.JabberChatService}.
 * FIXME How to test this stuff? XMPPConnection & C. require concrete classes' mocking
 * and thei constructors try to connect ... need to thing to higher level stuff.
 *
 * (See http://www.ociweb.com/jnb/jnbNov2005.html for some ideas with CGLib).
 */
public class JabberChatServiceTestCase extends MockObjectTestCase {
    /**
     * Verify tha messages are sent to right listeners.
     * @throws XMPPException 
     */
    public void testNotifyMessage() throws XMPPException {
        final String messageText = "Hello, world!"; 
        
        Mock mock = mock( Message.class );
        mock.stubs().method( "getType" ).withNoArguments()
            .will( returnValue( Message.Type.chat ) );
        mock.stubs().method( "getBody" ).withNoArguments()
            .will( returnValue( messageText ) );
        
        Message smackMessage = (Message) mock.proxy();
        
        mock = mock( IChatListener.class );
        mock.expects( once() ).method( "messageReceived" )
            .with( and( isA(IChatMessage.class), hasProperty("text", eq(messageText)) ) );
        IChatListener listener = (IChatListener) mock.proxy();
        
        ChatContext dummyContext = createDummyContext();// (Context) MockUtil.createDoNothingProxy( Context.class );
        JabberBackend dummyBackend = (JabberBackend) MockUtil.createDoNothingProxy( JabberBackend.class );
        
        JabberChatService chatService = new JabberChatService( dummyContext, dummyBackend );
        chatService.addChatListener( listener );
        
        chatService.processPacket( smackMessage );
    }
    
    private ChatContext createDummyContext() {
//        Mock mock = mock( IBuddy.class );
//        mock.stubs().method( "getId" ).withNoArguments()
//            .will( returnValue( "pippo" ) );
//        
//        Context context = new Context( (IBuddy) mock.proxy() );
//        return context;
        return (ChatContext) MockUtil.createDoNothingProxy( ChatContext.class );
    }
    
//    private JabberBackend createDummyJabberBackend() throws XMPPException {
//
//        Mock mockChat = mock( Chat.class, 
//                new Class[] {XMPPConnection.class, String.class }, 
//                new Object[] { new XMPPConnection( "dummyService" ), "pippo" } );
//        mockChat.expects( once() ).method( "addMessageListener" ).withAnyArguments();
//        Chat chat = (Chat) mockChat.proxy();
//
//        Mock mockConnection = mock( XMPPConnection.class, 
//                new Class[] { String.class }, 
//                new Object[] { "mockService" } );
//        mockConnection.expects( once() ).method( "createChat" ).withAnyArguments()
//            .will( returnValue( chat ) );
//        XMPPConnection connection = (XMPPConnection) mockConnection.proxy();
//        
//        Mock mock = mock( JabberBackend.class );
//        mock.expects( once() ).method( "getConnection" )
//            .will( returnValue( connection ) );
//
//        JabberBackend backend = (JabberBackend) mock.proxy();
//
//        return backend;
//    }
}
